var url = "https://item.taobao.com/item.htm?id=566531456886&ali_refid=a3_430582_1006:1109485102:N:64%E6%89%8B%E5%BC%8F+%E4%B8%AD%E5%9B%BD%E5%8F%AF%E5%8F%91%E5%B0%84:a2379d1438502037e9e2a1c9ef0d3a10&ali_trackid=1_a2379d1438502037e9e2a1c9ef0d3a10&spm=a230r.1.14.11#detail";
var url = "https://item.taobao.com/item.htm?spm=a21id.103458.512479.10.7814535dYfgv91&id=45808430645&scm=1007.12144.81309.6745_0&pvid=a8590167-6d47-46fe-80ae-1682986c0e4d&utparam=%7B%22x_hestia_source%22%3A%226745%22%2C%22x_mt%22%3A8%2C%22x_object_id%22%3A45808430645%2C%22x_object_type%22%3A%22item%22%2C%22x_pos%22%3A5%2C%22x_pvid%22%3A%22a8590167-6d47-46fe-80ae-1682986c0e4d%22%2C%22x_src%22%3A%226745%22%7D";
var page = require('webpage').create();
var retJsonpObj = null;

var onSibRequestSuccess = function(obj){
	retJsonpObj = obj;
};

page.captureContent = [ /.*/ ];
page.settings.loadImages = false;

page.onResourceReceived = function(response) {
	if(response.url.indexOf('detail/sib.htm')>-1){
		//这里回调onSibRequestSuccess方法
		eval(response.body);
	}
};
page.onError = function(msg,trace){
	var msgStack = ['ERROR: ' + msg];
	if (trace && trace.length) {
		msgStack.push('TRACE:');
		trace.forEach(function(t) {
			msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function + '")' : ''));
		});
	}
	// uncomment to log into the console 
	//console.error(msgStack.join('\n'));
	//phantom.exit();
};
page.open(url,function(status){
	var analyzeObj = page.evaluate(function(){

		//页面中dom提取的信息=========================================================
		var pageTitle = document.querySelector("#J_Title > h3").textContent;	//页面中标题
		var default_img = document.querySelector("#J_ImgBooth").src;	//页面中默认图片地址

		var objArr = [];
		var dls = document.querySelectorAll("div.tb-skin > dl.tb-prop");  //查找所有dl
		for(var a=0;a<dls.length;a++){
			var dlElement = dls[a];
			var dt = dlElement.querySelector("dt"); //查找属性类别
			
			var dataValues = dlElement.querySelectorAll("dd > ul > li");  //所有data-value
			var dataValueArr = [];
			for(var b=0;b<dataValues.length;b++){
			var dataValue = dataValues[b].getAttribute("data-value");
			var title = dataValues[b].querySelector("a > span").textContent;
			dataValueArr[b] = {key:dataValue,value:title};
			}
			objArr[a] = {attrTypes:dt.textContent,dataValues:dataValueArr};
		}
		
		var obj = {pageTitle:pageTitle,detailAttr:objArr,defaultImg:default_img}
		return obj;
	});
	var retObj = {};
	retObj.platform = 'taobao';
	retObj.jsonpObj = retJsonpObj;
	retObj.analyzeObj = analyzeObj;
	console.log(JSON.stringify(retObj));
	phantom.exit();
});


